package com.example.star.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.star.R;
import com.example.star.entity.Perimeter;

import java.util.ArrayList;
import java.util.List;

public class PerimeterAdapter extends ArrayAdapter<Perimeter> {
    private Context mContext;
    private int mResource;
    private List<Perimeter> originalList; // 存储原始数据
    private List<Perimeter> filteredList; // 存储过滤后的数据

    // 构造函数，用于初始化适配器
    public PerimeterAdapter(Context context, int resource, List<Perimeter> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.mResource = resource;
        this.originalList = new ArrayList<>(objects);
        this.filteredList = new ArrayList<>(objects);
    }

    @Override
    public int getCount() {
        return filteredList.size();
    }

    @Override
    public Perimeter getItem(int position) {
        return filteredList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        // 如果 convertView 为空，需要进行初始化
        if (convertView == null) {
            // 从上下文获取布局填充器，并填充布局
            convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
            viewHolder = new ViewHolder();
            // 查找布局中的各个视图组件
            viewHolder.imageView = convertView.findViewById(R.id.image_perimeter);
            viewHolder.starNameTextView = convertView.findViewById(R.id.star_name);
            viewHolder.perimeterNameTextView = convertView.findViewById(R.id.perimeter_name);
            viewHolder.pricesTextView = convertView.findViewById(R.id.prices);
            // 将 ViewHolder 存储在 convertView 的标签中，以便后续复用
            convertView.setTag(viewHolder);
        } else {
            // 若 convertView 不为空，从标签中获取 ViewHolder
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // 获取当前位置的 Perimeter 对象
        Perimeter perimeter = getItem(position);
        if (perimeter != null) {
            // 先设置默认图片
            viewHolder.imageView.setImageResource(R.drawable.register);

            // 检查 perimeter.getImageResource() 是否为 null
            if (perimeter.getImageResource() != null) {
                try {
                    // 尝试当作本地资源 ID 处理
                    int imageResId = mContext.getResources().getIdentifier(perimeter.getImageResource(), "drawable", mContext.getPackageName());
                    if (imageResId != 0) {
                        viewHolder.imageView.setImageResource(imageResId);
                    } else {
                        // 若不是有效的本地资源 ID，尝试使用 Glide 加载，可能是网络图片
                        Glide.with(mContext)
                                .load(perimeter.getImageResource())
                                .placeholder(R.drawable.register) // 加载中显示的图片
                                .error(R.drawable.register) // 加载失败显示的图片
                                .into(viewHolder.imageView);
                    }
                } catch (Exception e) {
                    // 出现异常，使用 Glide 加载，可能是网络图片
                    Glide.with(mContext)
                            .load(perimeter.getImageResource())
                            .placeholder(R.drawable.register)
                            .error(R.drawable.register)
                            .into(viewHolder.imageView);
                }
            }

            // 设置 TextView 的文本内容
            viewHolder.starNameTextView.setText(perimeter.getStarName());
            viewHolder.perimeterNameTextView.setText(perimeter.getTitle());
            viewHolder.pricesTextView.setText(String.valueOf(perimeter.getPrices()));
        }
        return convertView;
    }

    // 过滤方法
    public void filter(String keyword) {
        filteredList.clear();
        if (keyword.isEmpty()) {
            filteredList.addAll(originalList);
        } else {
            for (Perimeter perimeter : originalList) {
                if (perimeter.getTitle().toLowerCase().contains(keyword.toLowerCase()) ||
                        perimeter.getStarName().toLowerCase().contains(keyword.toLowerCase())) {
                    filteredList.add(perimeter);
                }
            }
        }
        notifyDataSetChanged();
    }

    // 内部类 ViewHolder，用于缓存视图组件
    private static class ViewHolder {
        ImageView imageView;
        TextView starNameTextView;
        TextView perimeterNameTextView;
        TextView pricesTextView;
    }
}